<?php
try {
	@header ( 'Content-type:text/html;charset=UTF-8' );

	define ( 'MacTop', true );
	define ( 'Root', dirname ( __FILE__ ) );
	require_once Root . '/../common.inc.php';
	require_once Root . '/../function.inc.php';
	
	$hour=intval(date('H'));
	if($hour<8 || $hour>21)
		exit;


	GetMyConnection ();
	
	require_once Root.'/smtp.php';

   
	   $sql = 'select * from mailqueue where issent=0 order by level desc,mailid asc limit 1';
	   $res = mysql_query ( $sql, $db );
	   $row = mysql_fetch_array ( $res );
	   if(!$row){
		 CleanUpDB ();
		 exit;
	   }
   
	$smtpserver     = "smtp.exmail.qq.com";//SMTP服务器
	$smtpserverport = 25;//SMTP服务器端口
	$smtpusermail   = "service@yunxiaoche.com";//SMTP服务器的用户邮箱
	$smtpemailto    = $row['tomail'];//发送给谁
	$smtpuser       = "service@yunxiaoche.com";//SMTP服务器的用户帐号
	$smtppass       = "Asdfgxfc123";//SMTP服务器的用户密码
	$mailsubject    = iconv('UTF-8','GB2312', $row['subject']);//邮件主题
	$mailbody       = iconv('UTF-8','GB2312', $row['message']);//邮件内容
	$mailtype       = $row['htmlon']?"HTML":"TXT";//邮件格式（HTML/TXT）,TXT为文本邮件

##########################################

	$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.

	$smtp->debug = false;//是否显示发送的调试信息

	$mailbody_header="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><title>$mailsubject</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/></head><body style=\"margin: 0; padding: 0;\">";
	$mailbody_end='</body></html>';
	 $mailbody=$mailbody_header.$mailbody.$mailbody_end;

	$jieguo=$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
	if($jieguo){
	   $update_sql = 'update mailqueue set issent=1,senttime=unix_timestamp() where mailid="' . $row['mailid'] . '" limit 1';
	   mysql_query ( $update_sql, $db );
	}else{
		$update_sql = 'update mailqueue set issent=2 ,failures=failures+1,senttime=unix_timestamp() where mailid="' . $row['mailid'] . '" limit 1';
		mysql_query ( $update_sql, $db );
	}
	  CleanUpDB ();
	   
 } catch ( Exception $e ) {
 
	CleanUpDB ();
	echo $e->getMessage ();
	exit ();
}  
?>
